//-----------------------------------------------------------------------
// <copyright file="a_PromoDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_PromoDAOBase
    {
        #region Common methods
        public virtual a_Promo Createa_PromoFromReader(IDataReader reader)
        {
            a_Promo item = new a_Promo();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PromoID"))) item.PromoID = (int)reader["PromoID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProdID"))) item.ProdID = (int)reader["ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price"))) item.Price = (decimal)reader["Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateStart"))) item.DateStart = (DateTime)reader["DateStart"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateEnd"))) item.DateEnd = (DateTime)reader["DateEnd"];
                if (!reader.IsDBNull(reader.GetOrdinal("StoreID"))) item.StoreID = (int)reader["StoreID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Promo methods
            
        public virtual void Createa_Promo(a_Promo a_Promo)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Promos_ins");
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Promo.ProdID);
                database.AddInParameter(dbCommand, "@Price", DbType.Currency, a_Promo.Price);
                database.AddInParameter(dbCommand, "@DateStart", DbType.DateTime, a_Promo.DateStart);
                database.AddInParameter(dbCommand, "@DateEnd", DbType.DateTime, a_Promo.DateEnd);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Promo.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Promo.DomainID);
                database.AddOutParameter(dbCommand, "@PromoID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Promo.PromoID = (int)database.GetParameterValue(dbCommand, "@PromoID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Promo methods
        
        public virtual void Updatea_Promo(a_Promo a_Promo)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Promos_upd");            
                
                database.AddInParameter(dbCommand, "@PromoID", DbType.Int32, a_Promo.PromoID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Promo.ProdID);
                database.AddInParameter(dbCommand, "@Price", DbType.Currency, a_Promo.Price);
                database.AddInParameter(dbCommand, "@DateStart", DbType.DateTime, a_Promo.DateStart);
                database.AddInParameter(dbCommand, "@DateEnd", DbType.DateTime, a_Promo.DateEnd);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Promo.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Promo.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Promo methods
        public virtual void Deletea_Promo(int promoID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Promos_del");
                
                database.AddInParameter(dbCommand, "@PromoID", DbType.Int32, promoID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Promo methods
        
        public virtual a_Promo Geta_Promo(int promoID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Promos_sel");
                
                database.AddInParameter(dbCommand, "@PromoID", DbType.Int32, promoID);
                
                a_Promo a_Promo =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Promo = Createa_PromoFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Promo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_PromoList methods
        public virtual a_PromoCollection Geta_PromoList(a_PromoColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Promos_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_PromoCollection a_PromoCollection = new a_PromoCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Promo a_Promo = Createa_PromoFromReader(reader);
                        a_PromoCollection.Add(a_Promo);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_PromoCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_PromoCollection Geta_PromoList(a_PromoColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_PromoList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}